﻿﻿
using System;
using System.Threading;
using System.Collections.Generic;
using Newtonsoft.Json;      //引用命名空间 [dotnet add package Newtonsoft.Json]
using _Car2dEnv;


namespace Car2dRun
{
    public class MyClient
    {
        public static void run()
        {
            int max_run_times_per_round = 1000;        // 每回合最大运行次数

            // string play_model = "human";
            // string play_model = "net";
            // int time_interval = 100;
            int time_interval = 15;
            string play_model = "sample";

            var env = new Car2dEnv(time_interval: time_interval, play_model: play_model, max_run_times_per_round: max_run_times_per_round);

            int MAX_EPOCH = 10;

            // var state = env.reset(clear_console: false);
            // BdUtils.log($"state --- {state}");

            for (var i = 0; i < MAX_EPOCH; i++)
            {
                env.reset();
                // env.render();
                float total_epoch_reward = .0f;
                while (true)
                {
                    var action = env.choose_action();
                    var my_step_ret = env.step(action);
                    env.render();
                    BdUtils.log($" --- {i} --- {env.run_times} --- {my_step_ret} -------------");
                    total_epoch_reward += my_step_ret.reward;
                    if (my_step_ret.done)
                    {
                        BdUtils.log($" --- 第{i}个epoch总得分为: {total_epoch_reward}");
                        BdUtils.TimeUtils.sleep(1);
                        break;
                    }
                }
            }

            // for (var i = 0; i < 1000000; i++)
            // {
            //     var action = env.choose_action();
            //     // if (action == 0)
            //     //     continue;
            //     // BdUtils.log($"a[{i}] --- action: {action}, codename: {env.i2s_action_dc[action]}");

            //     var my_step_ret = env.step(action);
            //     BdUtils.log($"{env.} --- ", my_step_ret);
            //     if (my_step_ret.done)
            //     {
            //         break;
            //     }

            //     // env.reset();
            //     env.render();
            //     // BdUtils.TimeUtils.sleep(0.1);
            // }

            // BdUtils.log($"agent_pos: ", this.current_state.agent_pos.x, this.current_state.agent_pos.y);

            // env.init();
            // render();
            // var init_state = JsonConvert.DeserializeObject<InitialState>(init_state);
            // InitialState init_state = new InitialState(
            //     agent_pos: new int[] { 5, 5 },
            //     desination_pos: new int[] { 12, 12 },
            //     // time_interval: 20,
            //     screen_size: new int[] { 11, 22 }
            // );

            // InitialState init_state = new InitialState(new int[] { 111, 222 });
            // MyVector2 xy = new MyVector2(111, 222);
            // xy.x = 123123123;
            // BdUtils.log("xy.arr --- ", xy.arr, xy.x, xy.y, "---", xy);

            // var rand = new Random();

            // for (var i = 0; i < 100; i++)
            // {

            //     env.reset();
            //     BdUtils.log("init_state ~~~ ", env.init_state);
            //     // BdUtils.log($"random {i} ~~~ ", rand.Next(10), BdUtils.TimeUtils.get_time_int());
            //     // BdUtils.log($"random {i} ~~~ ", get_random_number(0, 10, 1));
            //     // BdUtils.log($"{i} === BdUtils.TimeUtils.get_unique_suffix()   ", BdUtils.TimeUtils.get_unique_suffix(random_suffix_len: 0), "--- GetTimeStampMs: ", BdUtils.TimeUtils.GetTimeStampMs());
            // }

            // BdUtils.log(JsonConvert.DeserializeObject<InitialState>(init_state));
        }

        public static void render()
        {

            int time_interval = 500;
            int agent_x = 10;   //石头x初始值
            int agent_y = 0;    //石头y初始值

            int desination_x = 15;
            int desination_y = 15;

            int screen_x = 20;
            int screen_y = 20;

            string current_view = "";

            Console.Clear();
            while (true)
            {
                current_view = "";
                for (int y = 0; y < screen_y; y++)
                {
                    for (int x = 0; x < screen_x; x++)
                    {
                        // 自身位置
                        if (x == agent_x && y == agent_y)
                        {
                            current_view += "0";
                            continue;
                        }

                        // 围墙
                        if (y == 0 || y == screen_y - 1)
                        {
                            current_view += "-";
                            continue;
                        }
                        if (x == 0 || x == screen_x - 1)
                        {
                            current_view += "|";
                            continue;
                        }

                        // 目标点
                        if (x == desination_x && y == desination_y)
                        {
                            current_view += "Y";
                            continue;
                        }

                        current_view += " ";
                    }
                    current_view += "\n";
                }

                agent_y++;   //石头y值+1

                Console.SetCursorPosition(0, 0);
                Console.WriteLine(current_view);

                if (agent_y >= screen_y)
                {
                    break;
                }
                System.Threading.Thread.Sleep(time_interval);   //停顿0.5秒
            }
            // Console.WriteLine("结束.");
        }
    }

}